package com.example.hszpad.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.bumptech.glide.Glide;
import com.example.hszpad.R;
import com.example.hszpad.adapter.BaseRecyclerAdapter;
import com.example.hszpad.adapter.SmartViewHolder;
import com.example.hszpad.bean.HistoryDataBean;
import com.example.hszpad.bean.MenuItemBean;
import com.example.hszpad.util.DateUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * A simple {@link Fragment} subclass.
 */
public class HSZViewFragmentHistory extends Fragment {
    private RecyclerView recyclerView;
    private BaseRecyclerAdapter<HistoryDataBean> recyclerChildAdapter;
    private List<HistoryDataBean> menuItemBeanChildList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hszview_fragment_history, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        menuItemBeanChildList = getData(40);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerChildAdapter = new BaseRecyclerAdapter<HistoryDataBean>(menuItemBeanChildList, R.layout.layout_view_history_item, null) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, HistoryDataBean model, int position) {
                holder.text(R.id.tv_id, String.valueOf(position + 1));
                holder.text(R.id.tv_date, model.time);
                holder.text(R.id.tv_data01, model.data01);
                holder.text(R.id.tv_data02, model.data02);
                holder.text(R.id.tv_data03, model.data03);
                holder.text(R.id.tv_data04, model.data04);
                holder.text(R.id.tv_data05, model.data05);
                holder.text(R.id.tv_data06, model.data06);
                holder.viewBgColorId(R.id.ll_content, R.color.colorWhite);
                if (model.id % 2 == 1) {
                    holder.viewBgColorId(R.id.ll_content, R.color.color_bg_history_title);
                }
            }
        };
        recyclerView.setAdapter(recyclerChildAdapter);
        return view;
    }

    private List<HistoryDataBean> getData(int num) {
        Random random = new Random();
        List<HistoryDataBean> list = new ArrayList<>();
        HistoryDataBean bean = null;
        for (int i = 0; i < num; i++) {
            bean = new HistoryDataBean();
            bean.id = i;
            bean.time = DateUtil.getNow();
            float val1 = (50 + random.nextInt(70)) / 100.0f;
            float val2 = (700 + random.nextInt(300)) / 10.f;
            float val3 = (250 + random.nextInt(70)) / 10.f;
            float val4 = (180 + random.nextInt(70)) / 100.f;
            float val5 = (280 + random.nextInt(70)) / 10.f;
            float val6 = (850 + random.nextInt(140)) / 10.f;
            bean.data01 = String.format("%.02fMpa", val1);
            bean.data02 = String.format("%.01f℃", val2);
            bean.data03 = String.format("%.01f℃", val3);
            bean.data04 = String.format("%.02fMpa", val4);
            bean.data05 = String.format("%.01f℃", val5);
            bean.data06 = String.format("%.01f℃", val6);
            list.add(bean);
        }
        return list;
    }
}
